package com.yunkazaixian.syntelecom.utils;

import com.alibaba.fastjson.JSON;
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;

import java.util.Iterator;
import java.util.Map;


public class DXUtils {


    //济南中导
    public final static String zdjndxappKey= "CE95622721";
    public final static String zdjndxsecret = "13c529390";
    public final static String zdjndxEID = "500000935290";


    //菏泽敏晨
    public final static String mchzdxappKey="OS17491356";
    public final static String mchzdxsecret = "5b8f36165";
    public final static String mchzdxEID= "980010437762";


    //菏泽中导
    public final static String zdoldappKey="MC09041842";
    public final static String zdoldsecret="4d537006c";
    public final static String zdoldEID="500000935290";





    public static <T> T post(String url, String appKey, Map<String, Object> params, JSON json, Class<T> t) throws Exception {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        String timestamp = SignUtil.getCurTimestamp();
        //String timestamp ="20230823061413";
        StringBuilder sb = new StringBuilder();
        if (params != null) {
            sb.append(SignUtil.order(maptoString(params)));
        }
        if (json != null) {
            sb.append(json.toString());
        }
        sb.append(getSecretKey(appKey)).append(timestamp);
        String before = sb.toString();
        String sign = SignUtil.signMD5(before);
        headers.add("AppKey", appKey);
        headers.add("Timestamp", timestamp);
        headers.add("Sign", sign);
        // 以什么方式提交，自行选择，一般使用json，或者表单
        headers.setContentType(MediaType.APPLICATION_JSON);
        //将请求头部和参数合成一个请求
        HttpEntity<String> entity = null;
        if (json != null) {
            entity = new HttpEntity<>(json.toString(), headers);
        } else {
            entity = new HttpEntity<>("", headers);
        }

//        System.out.println("before=====" + before);
//        System.out.println("AppKey========" + appKey);
//        System.out.println("Timestamp========" + timestamp);
//        System.out.println("Sign========" + sign);
        //执行HTTP请求，将返回的结构使用Response类格式化
        T res = null;
        if (params != null) {
            res = restTemplate.postForObject(url + "?" + maptoString(params), entity, t);
        } else {
            res = restTemplate.postForObject(url, entity, t);
        }

        System.out.println(url);
        System.out.println(res);
        return res;
    }


    public static <T> T get(String url, String appKey, Map<String, String> params, Class<T> t) throws Exception {
        RestTemplate client = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        String timestamp = SignUtil.getCurTimestamp();
        String bodyStr = "";
        String paramStr = maptoString(params);
        String reqStr = SignUtil.order(paramStr) + bodyStr;
        String sign = SignUtil.signMD5(reqStr + getSecretKey(appKey) + timestamp);
        headers.add("AppKey", appKey);
        headers.add("Timestamp", timestamp);
        headers.add("Sign", sign);
        HttpMethod method = HttpMethod.GET;
        // 以什么方式提交，自行选择，一般使用json，或者表单
        //headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        //将请求头部和参数合成一个请求
        HttpEntity<Map<String, String>> requestEntity = new HttpEntity<>(null, headers);
        //执行HTTP请求，将返回的结构使用Response类格式化

        ResponseEntity<T> response = client.exchange(url + "?" + paramStr, method, requestEntity, t);
        T res = response.getBody();
//        System.out.println("AppKey========"+appKey);
//        System.out.println("Timestamp========"+timestamp);
//        System.out.println("Sign========"+sign);
        System.out.println(url + "?" + paramStr);
//        System.out.println(headers);
        System.out.println(res);
        return res;
    }


    static String maptoString(Map map) {
        if (map == null || map.isEmpty()) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        Iterator<Map.Entry<Object, Object>> it = map.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<Object, Object> entry = it.next();
            Object key = entry.getKey();
            Object value = entry.getValue();
            sb.append(key + "=" + value + "&");
        }
        if (sb.length() > 1) {
            sb.delete(sb.length() - 1, sb.length());
        }
        return sb.toString();
    }

    public static String getSecretKey(String appKey) {
        switch (appKey) {
            case zdjndxappKey:
                return zdjndxsecret;

            case mchzdxappKey:
                return mchzdxsecret;

            case zdoldappKey:
                return zdoldsecret;
            default:
                return "";
        }
    }


    public static String getEID(String appKey) {
        switch (appKey) {
            case zdjndxappKey:
                return zdjndxEID;

            case mchzdxappKey:
                return mchzdxEID;

            case zdoldappKey:
                return zdoldEID;
            default:
                return "";
        }
    }


}
